<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getConfigs">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.configs" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="key" label="键" width="320" sortable>
			</el-table-column>
			<el-table-column prop="value" label="值" width="320" sortable>
			</el-table-column>
			<el-table-column prop="intro" label="描述" width="320" sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :current-page="query.currentPage" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>
		
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="键" prop="name">
					<el-input v-model="saveForm.key" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="值" >
					<el-input v-model="saveForm.value" ></el-input>
				</el-form-item>
				<el-form-item label="描述" >
					<el-input type="textarea" v-model="saveForm.intro" ></el-input>
				</el-form-item>
				<el-form-item label="type">
					<el-input v-model="saveForm.type" ></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				configs: [],
				checkAll:[],
				checkMenuAll:[],
				permissionTree:[],//获取所有一级权限权限以及二级权限
				menuTree:[],//获取所有一级权限权限以及二级权限
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				setPermissionVisible:false,//编辑用户权限页面
				setMenuVisible:false,//编辑菜单权限页面
				isIndeterminate:false,
				savePermissionVisible:false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				saveLoading:false,
				query:{
					currentPage:1,
					pageSize:5,
					keyValue:''
				},
				ConfigPermission:{
					ConfigId:null,
					permissionSns:[]//记录所有已选定的权限的sn的
				},
				ConfigMenu:{
					ConfigId:null,
					menuId:[]//记录所有已选定的权限的id的
				},
				pageInfo: {
					total:0,
					configs:[],
				},
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入标识', trigger: 'blur' }
					]
				},
				//编辑界面数据
				allPermissionSns:[],//装所有权限的一级权限以及 二级权限的
				allMenuSns:[],//装所有权限的一级权限以及 二级权限的
				saveFormVisible: false,//新增界面是否显示
				addLoading: false,

				saveFormRules: {
					key: [
						{ required: true, message: '请输入键', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
					id:null,
					key: "",
					value: "",
					type: "",
					intro: ""
				}

			}
		},
		methods: {
			handlePermission(index,row){
				this.setPermissionVisible=true;
				this.getPermissionTree();
				this.ConfigPermission.ConfigId=row.id;
				this.$http.get("/Config/permissionSns/"+row.id).then(result=>{
					result=result.data;
					console.log(result,"handlePermission");
					console.log(this.ConfigPermission,"this.ConfigPermission");
					if (result.success){
						this.ConfigPermission.permissionSns=result.object;
						// 获取已选中的长度
						let checkedCount = this.ConfigPermission.permissionSns.length;
						// 如果已选中的长度==所有权限的长度，则checkAll=true，也就是全选框被选中
						this.checkAll = checkedCount === this.allPermissionSns.length;
						// 如果已选中的长度>0并且已选中的长度<所有权限的长度，则全选框是一个横杠
						this.isIndeterminate = checkedCount > 0 && checkedCount < this.allPermissionSns.length;
					}
				})

			},
			handleMenu(index,row){
				this.setMenuVisible=true;
				this.getMenuTree();
				this.ConfigMenu.ConfigId=row.id;
				this.$http.get("/Config/menuId/"+row.id).then(result=>{
					result=result.data;
					console.log(this.menuTree," ConfigMenu.menuId");
					console.log(result," xx this.ConfigMenu");
					if (result.success){
						for (let i=0;i<result.object.length;i++){
							this.ConfigMenu.menuId.push(parseInt(result.object[i]))
						}
					}
					console.log(this.ConfigMenu," this.ConfigMenu");
				})
			},
			getPermissionTree(){
				this.$http.get("/permission/tree").then(result=>{
					result=result.data;
					if (result.success){
						this.permissionTree=result.object;
						if (this.permissionTree&&this.permissionTree.length>0){
							for (let i=0;i<this.permissionTree.length;i++){
								this.allPermissionSns.push(this.permissionTree[i].sn);
								let child = this.permissionTree[i].children;
								for (let j=0;j<child.length;j++){
									this.allPermissionSns.push(child[j].sn);
								}
							}
						}
					}else {
						this.$message({
							message: '权限列表获取失败'+result.message,
							type: 'error'
						});
					}
				})
			},
			getMenuTree(){
				this.$http.get("/menu/tree").then(result=>{
					result=result.data;
					if (result.success){
						this.menuTree=result.object;
						if (this.menuTree&&this.menuTree.length>0){
							for (let i=0;i<this.menuTree.length;i++){
								this.allMenuSns.push(this.menuTree[i].id);
							}
						}
					}else {
						this.$message({
							message: '菜单列表树获取失败'+result.message,
							type: 'error'
						});
					}
				})
			},
			saveConfigMenu(){
				this.$confirm('确认提交吗？', '提示', {}).then(()=>{
					this.saveLoading = true;
					console.log(this.ConfigMenu,"this.ConfigMenu");
					this.$http.put("/Config/menu",this.ConfigMenu).then(result=>{
						result=result.data;
						this.saveLoading = false;
						this.setMenuVisible=false;
						if(result.success){
							this.$message({
								message: '修改成功',
								type: 'success'
							});
						}else {
							console.log(result.message)
							this.$message({
								message: '修改失败'+result.message,
								type: 'error'
							});
						}
					}).catch(()=>{
						this.$message({
							message: '网络波动',
							type: 'error'
					})
				})
			})
			},
			saveConfigPermission(){
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							this.$http.put("/Config/permission",this.ConfigPermission).then(result=>{
								result=result.data;
								this.saveLoading = false;
								this.setPermissionVisible=false;
								if(result.success){
									this.$message({
										message: '修改成功',
										type: 'success'
									});
								}else {
									console.log(result.message)
									this.$message({
										message: '修改失败'+result.message,
										type: 'error'
									});
								}
							}).catch(()=>{
								this.$message({
									message: '网络波动',
									type: 'error'
								});
							})
							}).catch(()=>{

			})
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getConfigs();
			},
			//获取用户列表
			getConfigs() {
				this.$http.post("/config",this.query)
						.then(result=>{
							result=result.data;
							console.log(result,"getconfig")
							this.pageInfo.total=result.object.size;
							this.pageInfo.configs=result.object.rows;
						})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(row.id)
					this.$http.delete("/config/"+row.id).then(result=>{
						this.listLoading=false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getConfigs();
						}
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
					saveForm: {
						key: "",
						value: "",
						type: "",
						intro: ""
					}
				};
			},
			//新增
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							this.saveFormVisible=true;
							//NProgress.start();
							let para=Object.assign({}, this.saveForm);
							console.log(para)
							this.$http.put("/config",this.saveForm).then(result=>{
								this.addLoading=false;
								result=result.data;
								console.log(result," Config")
								if (result.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.saveFormVisible=false;
									this.getConfigs();
								}else {
									this.$message({
										message: '提交失败',
										type: 'error'
									});
								}
							}).catch(()=>{
								this.$message({
									message: '网络波动',
									type: 'error'
								});
							})
						});
					}
				});
			},
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(ids)
					this.$http.patch("/config",ids).then(result=>{
						this.listLoading = false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '批量删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getConfigs();
						}else{
							this.$message({
								message: '批量删除失败'+result.message,
								type: 'error'
							});
						}
					})

				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getConfigs();
		}
	}

</script>

<style scoped>

</style>